package com.mj.postcode.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 邮编查询
 * @author zhaominglei
 * @date 2015-11-10
 * 
 */
public class Postcode implements Serializable{
	private static final long serialVersionUID = 6443028453322834945L;

	private String province; //省份/直辖市
	private String city; //市
	private String district; //区、县
	private String address; //街道、乡镇
	private String zipcode; //邮编
	
	public String getProvince() {
		return province;
	}
	public void setProvince(String province) {
		this.province = province;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getDistrict() {
		return district;
	}
	public void setDistrict(String district) {
		this.district = district;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getZipcode() {
		return zipcode;
	}
	public void setZipcode(String zipcode) {
		this.zipcode = zipcode;
	}
	
	/**
	 * 拆分街道、乡镇
	 */
	public List<Postcode> getPostcodes() {
		if (address != null && !address.equals("")) {
			List<Postcode> postcodes = new ArrayList<Postcode>();
			if (address.indexOf("|||") > -1) {
				String[] strings = address.split("\\|\\|\\|");
				if (strings != null && strings.length > 0) {
					for (String string : strings) {
						Postcode postcode = new Postcode();
						postcode.setProvince(getProvince());
						postcode.setCity(getCity());
						postcode.setDistrict(getDistrict());
						postcode.setAddress(string.replaceAll("\\(.+?\\)", ""));
						postcode.setZipcode(getZipcode());
						if (!postcodes.contains(postcode)) {
							postcodes.add(postcode);
						}
					}
					return postcodes;
				}
			} else {
				Postcode postcode = new Postcode();
				postcode.setProvince(getProvince());
				postcode.setCity(getCity());
				postcode.setDistrict(getDistrict());
				postcode.setAddress(address);
				postcode.setZipcode(getZipcode());
				postcodes.add(postcode);
				return postcodes;
			}
		}
		return null;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((address == null) ? 0 : address.hashCode());
		result = prime * result + ((city == null) ? 0 : city.hashCode());
		result = prime * result
				+ ((district == null) ? 0 : district.hashCode());
		result = prime * result
				+ ((province == null) ? 0 : province.hashCode());
		result = prime * result + ((zipcode == null) ? 0 : zipcode.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Postcode other = (Postcode) obj;
		if (address == null) {
			if (other.address != null)
				return false;
		} else if (!address.equals(other.address))
			return false;
		if (city == null) {
			if (other.city != null)
				return false;
		} else if (!city.equals(other.city))
			return false;
		if (district == null) {
			if (other.district != null)
				return false;
		} else if (!district.equals(other.district))
			return false;
		if (province == null) {
			if (other.province != null)
				return false;
		} else if (!province.equals(other.province))
			return false;
		if (zipcode == null) {
			if (other.zipcode != null)
				return false;
		} else if (!zipcode.equals(other.zipcode))
			return false;
		return true;
	}
}
